import java.util.LinkedList;
import java.util.Queue;

import edu.purdue.cs.cs180.channel.Channel;

/**
 * project7
 *
 * brief description of the program
 *
 * This program is released to public domain according to
 * author's wish, you can view full license at
 * http://tomatoeskit.org/upload/PublicDomainLicense.txt
 *
 * @author Fangning CHENG (Alpha.L Cheng) <chengf@purdue.edu>
 *
 * @lab section number 
 *
 * @date Nov 9, 2013
 *
 */

public class UrgencyServer extends Server {
    Queue<EndPointInfo> normalRequesters = new LinkedList<EndPointInfo>();
    Queue<EndPointInfo> urgentRequesters = new LinkedList<EndPointInfo>();
    Queue<EndPointInfo> emergRequesters  = new LinkedList<EndPointInfo>();
    Queue<EndPointInfo> volunteers       = new LinkedList<EndPointInfo>(); 
    
    
    public UrgencyServer(Channel cnl) {
        super(cnl);
        System.err.println("\tUrgency Server is running ...");
    }

    @Override
    public void messageReceived(String message, int clientID) {
        String msg[] = message.split("\\s+");
        int msgLen = msg.length;

        System.err.println(String.format("\t\tMessage from %d: %s", clientID, message));
        
        // Invalid message
        if (msgLen < 2)
            return;

        // Request message
        if (msg[0].equals("REQUEST")) {
            if (msgLen < 3) {
                System.err.println("\t\t\tBroken REQUEST message");
                return;
            }
            if (msg[2].equals("NORMAL"))
                normalRequesters.offer(new EndPointInfo(msg[1], clientID, msg[2]));
            
            if (msg[2].equals("URGENT"))
                urgentRequesters.offer(new EndPointInfo(msg[1], clientID, msg[2]));
            
            if (msg[2].equals("EMERGENCY"))
                emergRequesters.offer(new EndPointInfo(msg[1], clientID, msg[2]));
            
            System.err.println(String.format("\t\t\tRequester %d added to Queue %s", clientID, msg[2]));
        }

        // Volunteer message
        if (msg[0].equals("VOLUNTEER")) {
            volunteers.offer(new EndPointInfo(msg[1], clientID));
            System.err.println(String.format("\t\t\tVolunteer %d added to Queue", clientID));
        }

        // Process the queue
        processQueue();
    }

    private void processQueue() {
        if (volunteers.isEmpty())
            return;
        
        EndPointInfo requester = emergRequesters.isEmpty() ? urgentRequesters.isEmpty() ? normalRequesters.poll() 
                : urgentRequesters.poll() : emergRequesters.poll();
        
        if (requester == null)  // Three queues are empty
            return;
        
        EndPointInfo volunteer = volunteers.poll();
        
        sendMessages(volunteer.clientID, requester.clientID, timeTable.get(requester.building).get(volunteer.building),
                requester.building, requester.urgency, volunteer.building);
    }
}
